package cn.tom.config;

import cn.tom.config.smsFilter.SmsCodeAuthenticationSecurityConfig;
import cn.tom.dao.RoleDao;
import cn.tom.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;

import javax.annotation.Resource;

@Configuration
@EnableWebSecurity 
public class SecunityConfig {
    @Resource
    private AuthenticationConfiguration authenticationConfiguration;
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private UserDao userDao;
    @Resource
    private RoleDao roleDao;
    @Resource
    private UsernamePasswordAuthenticationSecurityConfig usernameConfig;
    @Resource
    private SmsCodeAuthenticationSecurityConfig smsConfig;

    @Bean
    PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Bean
    SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http.apply(usernameConfig)
                .and().
                apply(smsConfig)
                .and()
                .authorizeRequests()
                //前端static放开权限
                .antMatchers("/","/css/**","/fonts/**","/js/**","/img/**")
                .permitAll()
                .antMatchers("/api/login")
                .permitAll()
                .antMatchers("/api/user/**")
                .hasAnyAuthority("usr","admin")
                .antMatchers("/api/fore/**")
                .hasAuthority("usr")
                .antMatchers("/api/adm/**")
                .hasAuthority("admin")
                .antMatchers("/api/public/**")
                .permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginProcessingUrl("/api/login")
                .permitAll()
                .and()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .csrf().disable()
                .addFilter(
                        new JwtAuthoriztionFiler(authenticationConfiguration.getAuthenticationManager()
                                                ,redisTemplate,userDao,roleDao)
                )
                .exceptionHandling().authenticationEntryPoint(
                        new JwtAuthenticationEntryPoint()
                )
                .accessDeniedHandler(
                        new JwtAccessDeniedHandler()
                );
        return http.build();
    }
}
